'''
Created on 20/01/2010

@author: Simon
'''
#import sys
from urllib2 import HTTPError, URLError
import urllib
import urllib2


class HandleService:

   def _init_(self):
       print "initialize ..."

   def mint(self, authType, identifier, authDomain, appId,mintURL, hdlURL):
       #construct an user authentication info
       body = "<request name=\"addValue\">\n\
<properties>\n\
<property name=\"authType\" value=\"" + authType + "\"/>\n\
<property name=\"identifier\" value=\"" + identifier + "\"/>\n\
<property name=\"authDomain\" value=\"" + authDomain + "\"/>\n\
<property name=\"appId\" value=\"" + appId + "\"/>\n\
</properties>\n\
</request>"

       #Parameters for Handle Service
       #params = [('type',"URL"), ('value', hdlURL)]

       #Handle Server Connection URL
       #postString = mintURL + "?" + urllib.urlencode(params)
       postString = mintURL + hdlURL

       #send the request to handle service
       return self.request(postString, body)

   def request(self, postString, body):
       #connect to Handle Service
       req = urllib2.Request(postString, body)
       #open the connection
       try:
           response = urllib2.urlopen(req)
       except HTTPError, e:
           print('The server could not fulfill the request.')
           print('exception: ', e.code)
       except URLError, e:
           print('failed to reach a server.')
           print('Reason: ', e.reason)
       else:
           data = response.read()
           #read the response
           #result = ''
           #while 1:
           #    data = response.read(1024)
           #    if not len(data):
           #        break
           #        result = result + repr(data)
           #        sys.stdout.write(data)
           print ('Finished to sender  Handle Service')
       return data